package com.feidee.data.email.dao.impl;

import com.feidee.data.email.dao.ConfDao;
import com.feidee.data.email.model.EmailAddress;
import com.feidee.data.email.model.EmailConf;
import com.feidee.data.email.model.EmailQueryParam;
import com.feidee.data.report.exception.DataDaoException;
import com.feidee.data.report.util.JdbcManager;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@Repository("confDaoImpl")
public class ConfDaoImpl implements ConfDao {

  @Resource(name = "confDataSource")
  private DataSource confSource;


  @Override
  public List<EmailConf> listRunEmail() throws DataDaoException {
    Connection conn = null;
    Statement stat = null;
    ResultSet rs = null;
    List<EmailConf> list = new ArrayList<EmailConf>();
    try {
      conn = confSource.getConnection();
      String sql = "select email_key,is_enable,priority,run_date from email_conf where is_enable=1";
      stat = conn.createStatement();
      rs = stat.executeQuery(sql);
      while (rs.next()) {
        EmailConf conf = new EmailConf();
        conf.setEmail_key(rs.getString("email_key"));
        conf.setIs_enable(rs.getInt("is_enable"));
        conf.setPriority(rs.getInt("priority"));
        conf.setRun_date(rs.getString("run_date"));
        list.add(conf);
      }
      return list;
    } catch (Exception e) {
      throw new DataDaoException(e.getMessage(), e);
    } finally {
      JdbcManager.destroy(conn, stat, rs);
    }
  }

  @Override
  public EmailConf getEmailConf(String email_key) throws DataDaoException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      conn = confSource.getConnection();
      String sql = "select email_key,is_enable,priority,run_date,send_mail from email_conf where is_enable=1 and email_key=?";
      ps = conn.prepareStatement(sql);
      ps.setString(1, email_key);
      rs = ps.executeQuery();
      if (rs.next()) {
        EmailConf conf = new EmailConf();
        conf.setEmail_key(rs.getString("email_key"));
        conf.setIs_enable(rs.getInt("is_enable"));
        conf.setPriority(rs.getInt("priority"));
        conf.setRun_date(rs.getString("run_date"));
        conf.setSend_mail(rs.getInt("send_mail"));
        return conf;
      }
      return null;
    } catch (Exception e) {
      throw new DataDaoException(e.getMessage(), e);
    } finally {
      JdbcManager.destroy(conn, ps, rs);
    }
  }


  @Override
  public EmailAddress getEmailAddress(String email_key, String scene) throws DataDaoException {
    EmailAddress add = new EmailAddress();
    add.setScene(scene);
    add.setEmail_key(email_key);
    Connection conn = null;
    Statement stat = null;
    ResultSet rs = null;
    try {
      conn =confSource.getConnection();
      String sql = "select type,address from email_addresses where email_key='" + email_key + "'"
          + " and scene= '" + scene + "'";
      stat = conn.createStatement();
      rs = stat.executeQuery(sql);
      while (rs.next()) {
        String type = rs.getString("type");
        String address = rs.getString("address");
        add.put(type, address);
      }
      return add;
    } catch (Exception e) {
      e.printStackTrace();
      throw new DataDaoException(e.getMessage(), e);
    } finally {
      JdbcManager.destroy(conn, stat, rs);
    }
  }

  @Override
  public List<EmailQueryParam> getQueryParam(String email_key) throws DataDaoException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    List<EmailQueryParam> list = new ArrayList<EmailQueryParam>();
    try {
      conn = confSource.getConnection();
      String sql = "select email_key,email_index,email_operation,source_engine,source_sql,sink_type,sink_table,sink_keys," +
          "sink_data_check_class from email_sql where email_key=?";
      ps = conn.prepareStatement(sql);
      ps.setString(1,email_key);
      rs = ps.executeQuery();
      while (rs.next()) {
        EmailQueryParam conf = new EmailQueryParam();
        conf.setEmail_key(rs.getString("email_key"));
        conf.setEmail_index(rs.getInt("email_index"));
        conf.setEmail_operation(rs.getString("email_operation"));
        conf.setSource_engine(rs.getString("source_engine"));
        conf.setSource_sql(rs.getString("source_sql"));
        conf.setSink_type(rs.getString("sink_type"));
        conf.setSink_table(rs.getString("sink_table"));
        conf.setSink_keys(rs.getString("sink_keys"));
        conf.setSink_data_check_class(rs.getString("sink_data_check_class"));
        list.add(conf);
      }
      return list;
    } catch (Exception e) {
      throw new DataDaoException(e.getMessage(), e);
    } finally {
      JdbcManager.destroy(conn, ps, rs);
    }
  }


}
